<template>
  <div class="datamanager">
    <el-alert :title="result.message" :type="result.type" effect="dark" style="width:50%" v-if="result.show" :closable="false">
    </el-alert>
    <div style="margin: 15px 0;"></div>
    <el-card class="box-card">

      <div slot="header" class="clearfix">
        <h2>基础数据</h2>
        <div style="margin-top:10px"></div>
        <el-checkbox v-model="basicFlag">清除全部基础数据</el-checkbox>
      </div>
      <div v-if="!basicFlag">
        <el-checkbox :indeterminate="isIndeterminate" v-model="basicDatasCheckAll" @change="handleCheckAllChange">全选</el-checkbox>
        <div style="margin: 15px 0;"></div>
        <el-checkbox-group v-model="checkedbasicDatas" @change="handleCheckedbasicDatasChange">
          <el-checkbox v-for="item in basicDatas" :label="item.name" :key="item.name">{{item.name}}</el-checkbox>
        </el-checkbox-group>
      </div>
    </el-card>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <h2>业务数据</h2>
        <div style="margin-top:10px"></div>
        <el-checkbox v-model="businessFlag">清除全部业务数据</el-checkbox>
      </div>
      <div v-if="!businessFlag">
        <el-checkbox :indeterminate="isIndeterminate2" v-model="businessDatasCheckAll" @change="handleCheckAllChange2">全选</el-checkbox>
        <div style="margin: 15px 0;"></div>
        <el-checkbox-group v-model="checkedBusinessDatas" @change="handleCheckedBusinessDatasChange">
          <el-checkbox v-for="item in businessDatas" :label="item.name" :key="item.name">{{item.name}}</el-checkbox>
        </el-checkbox-group>
      </div>
      <div>
        <div style="margin: 15px 0;"></div>

        <el-date-picker v-model="startTime" type="datetime" placeholder="选择开始时间" value-format="yyyy-MM-dd HH:mm:ss"
          default-time="12:00:00">
        </el-date-picker>


        <el-date-picker v-model="endTime" type="datetime" placeholder="选择结束时间" value-format="yyyy-MM-dd HH:mm:ss"
          default-time="12:00:00">
        </el-date-picker>
        <span>(只针对业务数据)</span>
      </div>
    </el-card>
    <div style="margin-top:20px"></div>
    <el-button @click="submit()" type="primary">确定清除</el-button>
    <!-- <el-button @click="insert()">插入</el-button> -->
  </div>
</template>
<script>
  const basicDatas = [{
      name: '楼层数据',
      service: 'tHotelFloorService'
    },
    {
      name: '房间数据',
      service: 'tHotelFloorRoomService'
    },
    {
      name: '科目/商品/类目',
      service: 'tHotelFloorRoomConsumeService'
    },
    {
      name: '商品单位',
      service: 'tHotelFloorRoomConsumeCompanyService'
    },
    {
      name: '商品信息',
      service: 'tHotelFloorRoomConsumeShopService'
    },
    {
      name: '全天房规则',
      service: 'tHotelFloorRoomDayRuleService'
    },
    {
      name: '钟点房规则',
      service: 'tHotelFloorRoomHoursService'
    },
    {
      name: '规则与房型',
      service: 'tHotelFloorRoomHoursRoleService'
    },

    {
      name: '支付方式',
      service: 'tHotelFloorRoomPayTypeService'
    },
    {
      name: '房态设置',
      service: 'tHotelFloorRoomSettingService'
    },

    {
      name: '会员等级信息',
      service: 'hotelMemberLevelService'
    },
    {
      name: '会员标签',
      service: 'tHotelMemberLabelService'
    },
    {
      name: '会员分组',
      service: 'tHotelMemberGroupService'
    },
    {
      name: '非住客帐项目',
      service: 'nonRoomInoutTypeService'
    },
    {
      name: 'POS',
      service: 'tHotelCashBillsProjectService'
    },

  ];
  const businessDatas = [{
      name: '房间账务',
      service: 'tHotelFloorRoomCheckinAccountingService'
    },
    {
      name: '房间入住人记录',
      service: 'tHotelFloorRoomCheckinPeopleService'
    },
    {
      name: '入住类型',
      service: 'tHotelFloorRoomCheckinSingleService'
    },
    {
      name: '预定记录',
      service: 'tHotelFloorRoomSingleReserveService'
    },
    {
      name: '房间操作记录',
      service: 'tHotelFloorRoomLogService'
    },
    {
      name: '订单',
      service: 'hotelOrderService'
    },
    {
      name: '非住客账',
      service: 'nonRoomInoutService'
    },
    {
      name: 'POS消费',
      service: 'tHotelCashBillsService'
    },
    {
      name: '扫码付款明细',
      service: 'tHotelPreauthbarResultService'
    },
    {
      name: '交班记录',
      service: 'tHotelShiftWorkService'
    },
    {
      name: '续住记录',
      service: 'tHotelFloorRoomContinuedRecordService'
    },
    {
      name: '换房记录',
      service: 'tHotelFloorRoomChangeRecordService'
    },
    {
      name: '房间初始化',
      service: 'tHotelFloorRoomService'
    }
  ]

  export default {
    data() {
      return {
        basicDatasCheckAll: false,
        checked: [],
        checkedbasicDatas: [],
        basicDatas: basicDatas,
        isIndeterminate: true,

        basicFlag: false,

        businessDatasCheckAll: false,
        checked2: [],
        checkedBusinessDatas: [],
        businessDatas: businessDatas,
        isIndeterminate2: true,

        businessFlag: false,


        startTime: '',
        endTime: '',
        result: {
          show: false,
          type: '',
          message: "",
        }

      };
    },
    methods: {
      handleCheckAllChange(val) {
        const checked = this.basicDatas.map(function(item) {
          return item.name
        })
        this.checkedbasicDatas = val ? checked : [];
        this.checked = this.basicDatas
        this.isIndeterminate = false;


      },
      handleCheckedbasicDatasChange(value) {
        let checkedCount = value.length;
        this.basicDatasCheckAll = checkedCount === this.basicDatas.length;
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.basicDatas.length;


      },
      handleCheckAllChange2(val) {
        const checked2 = this.businessDatas.map(function(item) {
          return item.name
        })
        this.checkedBusinessDatas = val ? checked2 : [];
        this.checked2 = this.businessDatas
        this.isIndeterminate2 = false;
      },
      handleCheckedBusinessDatasChange(value) {
        let checkedCount = value.length;
        this.businessDatasCheckAll = checkedCount === this.businessDatas.length;
        this.isIndeterminate2 = checkedCount > 0 && checkedCount < this.businessDatas.length;
      },
      submit() {
        // console.log(this.checkedbasicDatas)
        // console.log(this.checkedBusinessDatas)

        var checkedbasicDatas = []

        for (let index in basicDatas) {
          for (let index2 in this.checkedbasicDatas) {
            if (basicDatas[index].name == this.checkedbasicDatas[index2]) {
              checkedbasicDatas.push(basicDatas[index])
            }
          }
        }

        var checkedBusinessDatas = []

        for (let index in businessDatas) {
          for (let index2 in this.checkedBusinessDatas) {
            if (businessDatas[index].name == this.checkedBusinessDatas[index2]) {
              checkedBusinessDatas.push(businessDatas[index])
            }
          }
        }

        console.log(this.startTime, this.endTime)

        if (this.basicFlag) {
          checkedbasicDatas = basicDatas
        }
        if (this.businessFlag) {
          checkedBusinessDatas = businessDatas
        }



        this.$confirm('此操作将永久删除选中数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/hotel/clear/data'),
            method: 'post',
            data: this.$http.adornData({
              basicData: checkedbasicDatas,
              businessData: checkedBusinessDatas,
              startTime: this.startTime,
              endTime: this.endTime
            })
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message.success(data.delBusinessData);
              // this.result.message=data.delBusinessData,
              // this.result.type="success"
              // this.result.show=true
            } else {
              this.$message.error(data.msg);
              // this.result.type="error"
              // this.result.message(data.msg)
              // this.result.show=true
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });

        console.log(checkedbasicDatas, checkedBusinessDatas)




      },
      // insert(){
      //   this.$http({
      //     url: this.$http.adornUrl('/hotel/clear/insert'),
      //     method: 'get',
      //   }).then(({
      //     data
      //   }) => {
      //     if (data && data.code === 0) {
      //         this.$message.success(data.code);
      //     } else {

      //     }
      //   })
      // }


    },
    mounted() {

    }
  };
</script>

<style lang="scss" scoped>
  .box-card {
    width: 50%;
    margin-bottom: 20px;
  }

  .el-checkbox+.el-checkbox {
    margin-left: 0px;
    margin-top: 10px;
  }

  .el-checkbox {
    width: 100px;
  }
  @media (max-width: 1400px) {
    .box-card {
      width: 60%;
    }
  }
</style>
